home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / audit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  2.5 KB  |  119 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #ifndef __CPLUSPLUS
  4. #include "mbuf.h"
  5.  
  6. extern char _Uend;
  7. extern int _STKRED;
  8.  
  9. union header {
  10.     struct {
  11.         union header *ptr;
  12.         unsigned size;
  13.     } s;
  14.     long l;
  15. };
  16.  
  17. static void
  18. dumpbuf(bp)
  19. struct mbuf *bp;
  20. {
  21.     union header *blk;
  22.  
  23.     if(bp == NULLBUF) {
  24.         tputs("NULL BUFFER\n");
  25.     } else {
  26.         blk = ((union header *)bp) - 1;
  27.         tprintf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  28.             (long)bp,
  29.             blk->s.size * sizeof(union header),
  30.             (long)bp->data,
  31.             bp->cnt,
  32.             (long)bp->next,
  33.             (long)bp->anext);
  34.     }
  35.     return;
  36. }
  37.  
  38. static void
  39. audit_mbuf(bp,file,line)
  40. struct mbuf *bp;
  41. char *file;
  42. int line;
  43. {
  44.     union header *blk;
  45.     char *bufstart, *bufend;
  46.     int16 datasize;
  47.     int errors = 0;
  48.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  49.     char *heapbot = &_Uend;
  50.     char *heaptop = (char *) -_STKRED;
  51.  
  52.     if(bp == NULLBUF)
  53.         return;
  54.  
  55.     /* Does buffer appear to be a valid malloc'ed block? */
  56.     blk = ((union header *)bp) - 1;
  57.     if(blk->s.ptr != blk){
  58.         tprintf("Garbage bp %lx\n",(long)bp);
  59.         errors++;
  60.     }
  61.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  62.         /* mbuf has data area associated with it, verify that
  63.          * pointers are within it
  64.          */
  65.         bufstart = (char *)(bp + 1);
  66.         bufend = (char *)bufstart + datasize;
  67.         if(bp->data < bufstart){
  68.             tputs("Data pointer before buffer\n");
  69.             errors++;
  70.         }
  71.         if(bp->data + bp->cnt > bufend){
  72.             tputs("Data pointer + count past bounds\n");
  73.             errors++;
  74.         }
  75.     } else {
  76.         /* Dup'ed mbuf, at least check that pointers are within
  77.          * heap area
  78.         */
  79.         if(bp->data < heapbot || bp->data + bp->cnt > heaptop){
  80.             tputs("Data outside heap\n");
  81.             errors++;
  82.         }
  83.     }
  84.     /* Now check link list pointers */
  85.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  86.          || bp->next > (struct mbuf *)heaptop)){
  87.             tputs("next pointer out of limits\n");
  88.             errors++;
  89.     }
  90.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  91.          || bp->anext > (struct mbuf *)heaptop)){
  92.             tputs("anext pointer out of limits\n");
  93.             errors++;
  94.     }
  95.     if(errors != 0){
  96.         dumpbuf(bp);
  97.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  98.         fflush(stdout);
  99.         exit(253);
  100.     }
  101.     return;
  102. }
  103.  
  104. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  105.  * nonzero otherwise
  106.  */
  107. void
  108. audit(bp,file,line)
  109. struct mbuf *bp;
  110. char *file;
  111. int line;
  112. {
  113.     struct mbuf *bp1;
  114.  
  115.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  116.         audit_mbuf(bp1,file,line);
  117. }
  118.  
  119. #endif /* __CPLUSPLUS */